package io.springboot.jpa.examples.springbootjpaexamples.config.druid;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yzc
 * @description Druid监控配置类
 * @date 2018/9/7
 */
@Configuration
public class DruidStatConfiguration {

    /**
     * 注册StatViewServlet
     */
    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        ServletRegistrationBean statViewServlet = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        // 设置白名单、黑名单
        statViewServlet.addInitParameter("allow", "127.0.0.1");
        // 优先级最高
        statViewServlet.addInitParameter("deny", "192.168.125.136");

        // 设置访问用户账号信息
        statViewServlet.addInitParameter("loginUsername", "admin");
        statViewServlet.addInitParameter("loginPassword", "admin");

        // 设置页面是否有重置数据的权限
        statViewServlet.addInitParameter("resetEnable", "false");
        return statViewServlet;
    }

    /**
     * 注册过滤器WebStatFilter
     */
    @Bean
    public FilterRegistrationBean druidStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.png,*.jpg,*.jpeg,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}
